import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class SerializationJavaServer extends Thread {
	private boolean throughputTest = false;
	private final int port;
	private static final int OBJECTS_COUNT = 10_000;

	public SerializationJavaServer(int port) {
		this.port = port;
	}

	public static void main(String[] args) {
		if (args.length < 1) {
			System.err.println("Invalid parameters list. Usage: SerializationJavaServer <port> ");
			System.exit(1);
		}
		int port = Integer.parseInt(args[0]);
		SerializationJavaServer server = new SerializationJavaServer(port);
		server.start();
	}

	public void run() {
		
		try (ServerSocket serverSocket = new ServerSocket(port);
				Socket socket = serverSocket.accept()) {
			ObjectOutputStream out = new ObjectOutputStream(socket.getOutputStream());
			out.flush();
			ObjectInputStream in = new ObjectInputStream(socket.getInputStream());
			for (int i = 0; i < OBJECTS_COUNT; i++) {
				SensorData sd = (SensorData) in.readObject();
				if (!throughputTest) {
					out.writeByte(1);
					out.flush();
				}
			}
			out.writeByte(0);
			out.flush();
		} catch (IOException | ClassNotFoundException e) {
			e.printStackTrace();
		}
	}
}
